package com.demo.login.web.controller;

import com.demo.login.commons.context.SpringContext;
import com.demo.login.entity.User;
import com.demo.login.service.UserService;
import com.demo.login.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginController extends HttpServlet {

    UserService userService = SpringContext.getBean(UserServiceImpl.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String email = req.getParameter("email");
        String password = req.getParameter("password");

        User user = userService.getUserByEmail(email);

        if (user == null) {
            req.setAttribute("message", "错误的用户名或密码");
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        } else {
            resp.sendRedirect("/main.jsp");
        }
    }
}